const { sequelize } = require('../utils/sequelize');
const { v4: uuidv4 } = require('uuid');
const handleMergeObj = require('../utils/mergeObj');
const ClassRoomService = require('./ClassRoomService');
const TeacherService = require('./TeacherService');
const CourseTypeService = require('./CourseTypeService');


class CourseService {
    constructor() {
    }

    async findAll(pageSize, pageNum, queryCriteria){
        let data = null;
        let offset = pageSize * (pageNum - 1);

        //设置获取条件
        let keyValArr = ['teacherId', 'categoryId', 'classRoomId'];
        let criteria = {};

        Object.keys(queryCriteria).forEach(function(key){
            if(keyValArr.includes(key) && queryCriteria[key] != ''){
                criteria[key] = queryCriteria[key];
            }
        })

        let courseArr = await sequelize.models.Course.findAndCountAll({
            offset,
            limit: pageSize * 1,
            where: criteria
        });
        let courseRows = courseArr.rows;
        for(let i=0;i<courseRows.length;i++){
            let cateory = await TeacherService.getAttrs(courseRows[i].teacherId, ['nickname', 'headUrl']);
            // console.log(cateory)
            courseRows[i].setDataValue('teacherName', cateory.getDataValue('teacherName'));
            courseRows[i].setDataValue('headUrl', cateory.getDataValue('headUrl'));
        }
        data = courseArr;

        return data;
    }

    async findAllAttrById(courseId){
        let data = null;
        let courseObj = await sequelize.models.Course.findOne({
            where: {course_id: courseId}});
        let roomObj = await ClassRoomService.findById(courseObj.classRoomId);
        let classTimeObj = await ClassTimeService.findById(courseObj.classTimeId);
        let user = await User.findAllAttrById(courseObj.teacherId);
        data = handleMergeObj(courseObj.dataValues, roomObj.dataValues, classTimeObj.dataValues, ['classRoomId', 'classTimeId', 'teacherId']);
        data.teacherName = user.nickname;

        return data;
    }

    async findAttrById(courseId, attrPara = []){
        let data = null;
        data = await sequelize.models.Course.findOne({
            attributes: attrPara,
            where: {course_id: courseId}});
        return data;
    }


    async saveCourse(course){
        let data = null;
        data = await sequelize.models.Course.create({
            courseId: uuidv4(),
            courseName: course.courseName,
            classRoomId: course.classRoomId,
            duration: course.duration,
            grade: course.grade,
            teacherId: course.teacherId,
            categoryId: course.categoryId,
            courseDesc: course.courseDesc,
            imgUrl: course.imgUrl,
            status: 0,
            classTimeId: course.classTimeId
        })
        return data;
    }

    async deleteCourse(courseId){
        let data = null;
        data = await sequelize.models.Course.destroy({where: {course_id: courseId}});
        return data;
    }

    async updateCourse(course){
        let data = null;
        let courseObj = {
            courseName: course.courseName,
            classRoomId: course.classRoomId,
            duration: course.duration,
            grade: course.grade,
            teacherId: course.teacherId,
            categoryId: course.categoryId,
            courseDesc: course.courseDesc,
            imgUrl: course.imgUrl,
            classTimeId: course.classTimeId
        }
        data = await sequelize.models.Course.update({...courseObj}, {
            where: {courseId: course.courseId}
        })
        return data;
    }

    async updateStatus(course) {
        let data = null;
        data = await sequelize.models.Course.update({status: course.status}, {
            where: {courseId: course.courseId}
        })
        return data;
    }

    async getAttr(courseId, attrArr) {
        let data = null;
        data = await sequelize.models.Course.findOne({
            attributes: attrArr,
            where: {courseId}
        })
        return data;
    }
}

module.exports = new CourseService();